package com.alcatel_lucent.osp.action;

import com.alcatel_lucent.osp.service.DaoService;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Base class of JSON-style actions.
 * By default it products json string containing status and message as action result.
 * Subclass is responsible to set status and message before return.
 * Subclass should always return SUCCESS, if action failed, it set status with proper error code.
 *
 * @author allany
 */
@ParentPackage("osp-json")
@Result(type = "json", params = {
        "noCache", "true",
        "contentType", "json",
        "enableGZIP ", "true",
        "ignoreHierarchy", "false",
        "includeProperties", "status,message"}
)
@SuppressWarnings("serial")
abstract public class JSONAction extends BaseAction{
    protected int status;
    protected String message;
    protected String json;

    @Autowired
    protected DaoService dao;

    public void setStatus(int status) {
        this.status = status;
    }

    public int getStatus() {
        return status;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    abstract protected String performAction() throws Exception;

    public String execute() {
        try {
            return performAction();
        } catch (Exception e) {
            e.printStackTrace();
            setStatus(-1);
            setMessage(e.toString());
            return SUCCESS;
        }
    }
}
